package com.cleaningcloud.cld.service.impl.mini;

import com.cleaningcloud.cld.domain.CldTemporary;
import com.cleaningcloud.cld.domain.CldTemporaryReply;
import com.cleaningcloud.cld.domain.dto.CldProjectPostDto;
import com.cleaningcloud.cld.domain.vo.*;
import com.cleaningcloud.cld.mapper.CldProjectPostMapper;
import com.cleaningcloud.cld.mapper.CldStaffMapper;
import com.cleaningcloud.cld.mapper.CldTemporaryMapper;
import com.cleaningcloud.cld.mapper.CldTemporaryReplyMapper;
import com.cleaningcloud.cld.service.mini.IMiniTemporaryService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * packageName com.cleaningcloud.cld.service.impl.mini
 *
 * @author Shaw
 * @version JDK 8
 * @className MiniTemporaryService
 * @date 2025-06-19
 * @description 临时工作安排
 */
@Service
public class MiniTemporaryService implements IMiniTemporaryService {

    @Autowired
    private CldTemporaryMapper cldTemporaryMapper;
    @Autowired
    private CldTemporaryReplyMapper cldTemporaryReplyMapper;
    @Autowired
    private CldProjectPostMapper cldProjectPostMapper;
    @Autowired
    private CldStaffMapper cldStaffMapper;

    /**
     * @Author Shaw
     * @Description 新增临时工作安排
     * @date: 2025-06-19 9:26
     * @param: //TODO
     */
    @Override
    public int insertCldTemporary(CldTemporary cldTemporary)
    {
        cldTemporary.setInitiateTime(DateUtils.getNowDate());
        cldTemporary.setProjectAccountId(SecurityUtils.getUserId());
        cldTemporary.setHandleStatus(0L);
        cldTemporary.setFlag(0L);

        CldProjectPostDto dto = new CldProjectPostDto();
        dto.setProjectId(cldTemporary.getProjectId());
        dto.setProjectDuitsName(1L);
        List<CldProjectPostSetVo> list = cldProjectPostMapper.selectCldProjectPostList(dto);
        if(!list.isEmpty()){
            cldTemporary.setAssignStaffId(list.get(0).getStaffId().toString());
        }
        return cldTemporaryMapper.insertCldTemporary(cldTemporary);
    }

    /**
     * @Author Shaw
     * @Description 获取临时工作安排列表
     * @date: 2025-06-19 9:45
     * @param: //TODO
     */
    @Override
    public List<CldTemporaryVo> getTemporaryList(CldTemporaryVo cldTemporaryVo)
    {
        return cldTemporaryMapper.selectCldTemporaryList(cldTemporaryVo);
    }

    /**
     * @Author Shaw
     * @Description 根据工作安排ID查看详情
     * @date: 2025-06-19 10:40
     * @param: //TODO
     */
    @Override
    public CldTemporaryVo getTemporaryInfo(Long temporaryId)
    {
        CldTemporaryVo cldTemporaryVo = cldTemporaryMapper.selectCldTemporaryByTemporaryId(temporaryId);
        CldTemporaryReplyVo cldTemporaryReplyVo = new CldTemporaryReplyVo();
        cldTemporaryReplyVo.setTemporaryId(temporaryId);
        List<CldTemporaryReplyVo> cldTemporaryReplyVoList = cldTemporaryReplyMapper.selectCldTemporaryReplyList(cldTemporaryReplyVo);
        if(!cldTemporaryReplyVoList.isEmpty()){
            cldTemporaryVo.setReplyList(cldTemporaryReplyVoList);
        }
        return cldTemporaryVo;
    }

    /**
     * @Author Shaw
     * @Description  下发整改
     * @date: 2025-06-19 10:40
     * @param: //TODO
     */
    @Override
    public int IssueRectification(CldTemporaryVo cldTemporaryVo)
    {
        cldTemporaryVo.setAssignTime(DateUtils.getNowDate());
        cldTemporaryVo.setHandleStatus(1L);
        if(null != cldTemporaryVo.getHandleStaffId()){
            cldTemporaryVo.setHandleStaffId(cldTemporaryVo.getHandleStaffId());
            String[] staffIds = cldTemporaryVo.getHandleStaffId().split(",");
            for (String staffId : staffIds) {
                CldStaffVo obj = cldStaffMapper.selectCldStaffByStaffId(Long.valueOf(staffId));
                CldTemporaryReply cldTemporaryReply = new CldTemporaryReply();
                cldTemporaryReply.setReplyStaffId(obj.getStaffId());
                cldTemporaryReply.setReplyUserType(1L);
                cldTemporaryReply.setReplyContent("下发给" + obj.getStaffName());
                cldTemporaryReply.setIfStatus(0L);
                cldTemporaryReply.setTemporaryId(cldTemporaryVo.getTemporaryId());
                cldTemporaryReply.setReplyTime(DateUtils.getNowDate());
                cldTemporaryReplyMapper.insertCldTemporaryReply(cldTemporaryReply);
            }
        }
        return cldTemporaryMapper.updateCldTemporary(cldTemporaryVo);
    }

    /**
     * @Author Shaw
     * @Description 立即反馈
     * @date: 2025-06-20 9:33
     * @param: //TODO
     */
    @Override
    @Transactional
    public int insertCldTemporaryReply(CldTemporaryReply cldTemporaryReply) {
        cldTemporaryReply.setReplyTime(DateUtils.getNowDate());
        cldTemporaryReply.setReplyStaffId(SecurityUtils.getUserId());
        cldTemporaryReply.setIfStatus(1L);
        int i = cldTemporaryReplyMapper.insertCldTemporaryReply(cldTemporaryReply);
        boolean result = true;
        //反馈人为员工
        if(cldTemporaryReply.getReplyUserType() == 1){
            //获取下发人集合
            CldTemporaryReplyVo queryReplyVo = new CldTemporaryReplyVo();
            queryReplyVo.setTemporaryId(cldTemporaryReply.getTemporaryId());
            queryReplyVo.setReplyUserType(1L);
            queryReplyVo.setIfStatus(0L);
            List<CldTemporaryReplyVo> replyList = cldTemporaryReplyMapper.selectCldTemporaryReplyList(queryReplyVo);
            if(!replyList.isEmpty()){
                for (CldTemporaryReplyVo replyVo : replyList) {
                    queryReplyVo.setReplyStaffId(replyVo.getReplyStaffId());
                    queryReplyVo.setIfStatus(1L);
                    List<CldTemporaryReplyVo> list = cldTemporaryReplyMapper.selectCldTemporaryReplyList(queryReplyVo);
                    if(list.isEmpty()){
                        result = false;
                    }
                }
            }
        }
        if(result){
            // 修改工作安排状态为已反馈
            CldTemporary temporary = cldTemporaryMapper.selectCldTemporaryByTemporaryId(cldTemporaryReply.getTemporaryId());
            if (temporary != null) {
                if (temporary.getHandleStatus() == 1 || temporary.getHandleStatus() == 0) {
                    temporary.setHandleStatus(2L);
                    temporary.setHandleTime(DateUtils.getNowDate());
                    if(null != cldTemporaryReply.getZgStaffId()){
                        temporary.setHandleStaffId(cldTemporaryReply.getZgStaffId().toString());
                    }
                    temporary.setAssignStaffId(temporary.getAssignStaffId() == null ? SecurityUtils.getUserId().toString() : temporary.getAssignStaffId());
                    cldTemporaryMapper.updateCldTemporary(temporary);
                }
            }
        }
        return i;
    }

    /**
     * @Author Shaw
     * @Description 确认完成、撤销、驳回
     * @date: 2025-06-20 9:33
     * @param: //TODO
     */
    @Override
    @Transactional
    public int auditTemporary(CldTemporaryVo cldTemporaryVo) {
        CldTemporaryReply cldTemporaryReply = new CldTemporaryReply();
        cldTemporaryReply.setReplyStaffId(SecurityUtils.getUserId());
        cldTemporaryReply.setReplyTime(DateUtils.getNowDate());
        if (cldTemporaryVo.getHandleStatus() == 3) {
            cldTemporaryVo.setRejectTime(DateUtils.getNowDate());
            cldTemporaryVo.setHandleStatus(1L);
            cldTemporaryReply.setReplyContent(cldTemporaryVo.getRejectText());
            //获取已提交人集合
            CldTemporaryReplyVo queryReplyVo = new CldTemporaryReplyVo();
            queryReplyVo.setTemporaryId(cldTemporaryReply.getTemporaryId());
            queryReplyVo.setReplyUserType(1L);
            queryReplyVo.setIfStatus(1L);
            List<CldTemporaryReplyVo> replyList = cldTemporaryReplyMapper.selectCldTemporaryReplyList(queryReplyVo);
            if(!replyList.isEmpty()){
                for (CldTemporaryReplyVo replyVo : replyList) {
                    replyVo.setIfStatus(2L);//驳回
                    cldTemporaryReplyMapper.updateCldTemporaryReply(replyVo);
                }
            }
        }
        if (cldTemporaryVo.getHandleStatus() == 4) {
            cldTemporaryReply.setReplyContent(cldTemporaryVo.getSovleText());
        }
        if (cldTemporaryVo.getHandleStatus() == 5) {
            cldTemporaryReply.setReplyContent(cldTemporaryVo.getRevokeText());
        }
        cldTemporaryReply.setReplyUserType(3L);
        cldTemporaryReply.setTemporaryId(cldTemporaryVo.getTemporaryId());
        cldTemporaryReply.setReplyImgs(cldTemporaryVo.getRejectImg());
        cldTemporaryReplyMapper.insertCldTemporaryReply(cldTemporaryReply);
        // 修改工作安排
        return cldTemporaryMapper.updateCldTemporary(cldTemporaryVo);
    }
}